package com.zhulin.android.edoctor.fragment;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.zhulin.android.edoctor.R;
import com.zhulin.android.edoctor.db.DBDoctorDBHelp;
import com.zhulin.android.edoctor.jsonobject.JOCaseInfoModel;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class FragmentMyselftCaseList extends BaseFragment implements OnClickListener{

	private ListView lstViewCaseList;
	private TextView txtCaseTypeName;
	private boolean UploadedSelected=false;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v=inflater.inflate(R.layout.fragment_myselftcase_content, container, false);

		lstViewCaseList=(ListView)v.findViewById(R.id.lstVewCaseList);
		v.findViewById(R.id.btnUploaded).setOnClickListener(this);
		v.findViewById(R.id.btnNoUpload).setOnClickListener(this);
		
		txtCaseTypeName=(TextView)v.findViewById(R.id.txtMyselftcaseName);
		loadData(false,getString(R.string.case_type_01));
		return v;
	}

	PopupWindow mPopupWindow;
	private void showCaseTypeList(View anchor){
		View mViewPopup=LayoutInflater.from(getActivity()).inflate(R.layout.popup_case_type, null);
		mPopupWindow = new PopupWindow(mViewPopup, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_title_background));
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setOnDismissListener(null);
		mPopupWindow.update();
		mPopupWindow.showAsDropDown(anchor);

		addPopupItemClickListener(mViewPopup);
	}

	private OnClickListener clickListenerCaseType=new OnClickListener() {

		@Override
		public void onClick(View v) {
			if(mPopupWindow!=null && mPopupWindow.isShowing()){
				mPopupWindow.dismiss();
			}
			switch(v.getId()){
			case R.id.btnCaseType1:{
				loadData(UploadedSelected, getResources().getString(R.string.case_type_01));
				break;
			}
			case R.id.btnCaseType2:{
				loadData(UploadedSelected, getResources().getString(R.string.case_type_02));
				break;
			}
			case R.id.btnCaseType3:{
				loadData(UploadedSelected, getResources().getString(R.string.case_type_03));
				break;
			}
			case R.id.btnCaseType4:{
				loadData(UploadedSelected, getResources().getString(R.string.case_type_04));
				break;
			}
			case R.id.btnCaseType5:{
				loadData(UploadedSelected, getResources().getString(R.string.case_type_05));
				break;
			}
			case R.id.btnCaseType0:{
				loadData(UploadedSelected, getResources().getString(R.string.case_type_06));
				break;
			}
			}
			
			txtCaseTypeName.setText(((Button)v).getText().toString());
		}
	};

	private void addPopupItemClickListener(View v){
		
		v.findViewById(R.id.btnCaseType1).setOnClickListener(clickListenerCaseType);
		v.findViewById(R.id.btnCaseType2).setOnClickListener(clickListenerCaseType);
		v.findViewById(R.id.btnCaseType3).setOnClickListener(clickListenerCaseType);
		v.findViewById(R.id.btnCaseType4).setOnClickListener(clickListenerCaseType);
		v.findViewById(R.id.btnCaseType5).setOnClickListener(clickListenerCaseType);
		v.findViewById(R.id.btnCaseType0).setOnClickListener(clickListenerCaseType);
	}

	private void loadData(boolean hashUploaded,String type){
		List<JOCaseInfoModel> arrayList=null;
		DBDoctorDBHelp dbDoctorDBHelp=new DBDoctorDBHelp(getActivity());
		try {
			Dao<JOCaseInfoModel, Void> daoJOCaseInfo= dbDoctorDBHelp.getDaoMyselftCase();
			QueryBuilder<JOCaseInfoModel, Void> queryBuilder= daoJOCaseInfo.queryBuilder();
			if(type.equals(getResources().getString(R.string.case_type_01)))//如果选择的是全部,则不用筛选类型
			{
				queryBuilder.where().eq("hasUploaded", hashUploaded);
			}else
			{
				queryBuilder.where().eq("hasUploaded", hashUploaded).and().eq("name", type);
			}
			arrayList=daoJOCaseInfo.query(queryBuilder.prepare());
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			dbDoctorDBHelp.close();
			if(arrayList==null){
				arrayList=new ArrayList<JOCaseInfoModel>();
			}
		}

		MySimpleAdapter mySimpleAdapter=new MySimpleAdapter(arrayList);
		lstViewCaseList.setAdapter(mySimpleAdapter);
	}

	private class MySimpleAdapter extends BaseAdapter{

		List<JOCaseInfoModel> mData;
		public MySimpleAdapter(List<JOCaseInfoModel> joCaseInfoModels){
			this.mData=joCaseInfoModels	;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView=LayoutInflater.from(getActivity()).inflate(R.layout.item_caselist_subitem, parent,false);
			}

			TextView txtName		=			(TextView)convertView.findViewById(R.id.txtName);
			TextView txtPraiseCount	=			(TextView)convertView.findViewById(R.id.txtPraiseCount);
			TextView txtCommentCount=			(TextView)convertView.findViewById(R.id.txtCommentCount);
			TextView txtUpdateDate	=			(TextView)convertView.findViewById(R.id.txtUpdateDate);



			JOCaseInfoModel joCaseInfoModel=mData.get(position);
			txtName.setText(joCaseInfoModel.getName());
			txtPraiseCount.setText("赞:"+joCaseInfoModel.getPraiseCount()+"个");
			txtCommentCount.setText("评:"+joCaseInfoModel.getCommentCount()+"个");


			//String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(joCaseInfoModel.getUpdateDate());
			txtUpdateDate.setText("上传时间:"+joCaseInfoModel.getUpdateDate());

			return convertView;
		}

	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btnUploaded:{
			UploadedSelected=true;
			//loadData(true);
			break;
		}case R.id.btnNoUpload:{
			UploadedSelected=false;
			//loadData(false);
			break;
		}
		}
		showCaseTypeList(v);
	}
}
